package android.rockchip.update.download;

import android.app.DownloadManager;
import android.content.Context;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.os.AsyncTask;
import android.rockchip.update.utils.Constants;
import android.rockchip.update.utils.Preferences;
import android.util.Log;

public class DownloadRomProgress extends AsyncTask<Long, Integer, Void> implements Constants {

    public final String TAG = this.getClass().getSimpleName();

    private Context mContext;
    private DownloadManager mDownloadManager;

    public DownloadRomProgress(Context context, DownloadManager downloadManager) {
        mContext = context;
        mDownloadManager = downloadManager;
    }

    @Override
    protected Void doInBackground(Long... params) {
        int previousValue = 0;
        while (Preferences.getIsDownloadOnGoing(mContext)) {
            long mDownloadID = Preferences.getDownloadID(mContext);
            DownloadManager.Query q = new DownloadManager.Query();
            q.setFilterById(mDownloadID);

            Cursor cursor = mDownloadManager.query(q);
            cursor.moveToFirst();
            try {
                final int bytesDownloaded = cursor.getInt(cursor
                        .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                final int bytesInTotal = cursor.getInt(cursor
                        .getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

                if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
                        == DownloadManager.STATUS_SUCCESSFUL) {
                    Preferences.setIsDownloadRunning(mContext, false);
                }

                final int progressPercent = (int) ((bytesDownloaded * 100l) / bytesInTotal);

                if (progressPercent != previousValue) {
                    // Only publish every 1%, to reduce the amount of work being done.
                    publishProgress(progressPercent, bytesDownloaded, bytesInTotal);
                    previousValue = progressPercent;
                }
            } catch (CursorIndexOutOfBoundsException e) {
                Preferences.setIsDownloadRunning(mContext, false);
            } catch (ArithmeticException e) {
                Preferences.setIsDownloadRunning(mContext, false);
                Log.e(TAG, " " + e.getStackTrace());
            }
            cursor.close();
        }
        return null;
    }

    protected void onProgressUpdate(Integer... progress) {
        Log.d(TAG, "OTA Updating Progress - " + progress[0] + "%");
    }

}
